package com.yueyang.sojourn.server.service.sojourn.banner;

import com.yueyang.sojourn.server.model.enums.BannerShowTypeEnum;
import com.yueyang.sojourn.server.model.po.SojournBanner;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-26
 */
@Service
public class SojournBannerService {

    @Resource
    private SojournBannerMapper sojournBannerMapper;


    public int deleteByPrimaryKey(Long id) {
        return sojournBannerMapper.deleteByPrimaryKey(id);
    }


    public int insert(SojournBanner record) {
        return sojournBannerMapper.insert(record);
    }


    public int insertSelective(SojournBanner record) {
        return sojournBannerMapper.insertSelective(record);
    }


    public SojournBanner selectByPrimaryKey(Long id) {
        return sojournBannerMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(SojournBanner record) {
        return sojournBannerMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(SojournBanner record) {
        return sojournBannerMapper.updateByPrimaryKey(record);
    }

    /**
     * @param keyWord
     * @param showType        {@link BannerShowTypeEnum#values()}
     * @param showTimeBeginGT 开始时间大于showTimeBeginGT
     * @param showTimeBeginLT 开始时间小于showTimeBeginGT
     * @param showTimeEndGT   结束时间大于showTimeBeginGT
     * @param showTimeEndLT   结束时间小于showTimeBeginGT
     * @param sort            首页banner展示排序值
     * @param excludeIds
     * @return
     */
    public List<SojournBanner> selectList(String keyWord, Integer showType, Date showTimeBeginGT, Date showTimeBeginLT,
                                          Date showTimeEndGT, Date showTimeEndLT, Integer sort, List<Long> excludeIds) {
        if (showType != null) {
            if (BannerShowTypeEnum.TO_BE_SHOW.getCode() == showType) {
                showTimeBeginGT = new Date();
            } else if (BannerShowTypeEnum.SHOWING.getCode() == showType) {
                Date now = new Date();
                showTimeBeginLT = now;
                showTimeEndGT = now;
            } else if (BannerShowTypeEnum.OFF.getCode() == showType) {
                showTimeEndLT = new Date();
            } else {
                return Collections.emptyList();
            }
        }

        return sojournBannerMapper.selectList(keyWord, showTimeBeginGT, showTimeBeginLT, showTimeEndGT, showTimeEndLT, sort, excludeIds);
    }

    public Integer selectExistsSort(int sort, Date showTimeBegin, Date showTimeEnd) {
        return sojournBannerMapper.selectExistsSort(sort, showTimeBegin, showTimeEnd);
    }
}
